package com.cloud.order.controller;

import com.cloud.order.entity.Order;
import com.cloud.order.properties.OrderProperties;
import com.cloud.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * packageName com.cloud.order.controller
 *
 * @author yanshuanglong
 * @version JDK 8
 * @className OrderController
 * @date 2025/3/17
 * @description TODO
 */
@Slf4j
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderProperties orderProperties;

    @GetMapping("/getConfig")
    public String config(){
        return "order.timeout: "+orderProperties.getTimeout()+", order.auto-confirm:"
                +orderProperties.getAutoConfirm()+", order.db-url:"+orderProperties.getDbUrl();
    }

    @GetMapping("/create")
    private Order create(@RequestParam("productId") Long productId , @RequestParam("userId") Long userId) {

        return orderService.createOrder(productId,userId);
    }

    @GetMapping("/createSeckill")
    private Order createSeckill(@RequestParam("productId") Long productId , @RequestParam("userId") Long userId) {
        Order order = orderService.createOrder(productId, userId);
        order.setId(Long.MAX_VALUE);
        return order;
    }
    @GetMapping("/readDb")
    public String readDb(){
        log.info("readDb........");
        return "read: "+orderProperties.getDbUrl();
    }
    @GetMapping("writeDb")
    public String writeDb(){
        log.info("writeDb.......");
        return "write: "+orderProperties.getDbUrl();
    }
}
